/**
 * Created by douxin on 23/09/2016.
 */

import CityLatLng from './CityLatLng.json';

export function getChannelStatisticData(trades) {
    let result = [{
        title: '一口价',
        key: 'FIXED',
        value: 0
    }, {
        title: '送礼',
        key: 'GIFT',
        value: 0
    }, {
        title: '分销商采购',
        key: 'BULK_PURCHASE',
        value: 0
    }, {
        title: '赠品领取',
        key: 'PRESENT',
        value: 0
    }, {
        title: '拼团订单',
        key: 'GROUP',
        value: 0
    }, {
        title: '货到付款',
        key: 'COD',
        value: 0
    }, {
        title: '代付',
        key: 'PEER',
        value: 0
    }, {
        title: '扫码商家二维码直接支付的交易',
        key: 'QRCODE',
        value: 0
    }, {
        title: '线下收银台二维码交易',
        key: 'QRCODE_3RD',
        value: 0
    }];

    trades.forEach((val, index) => {
        result.forEach((rv, ri) => {
            if (val.type && rv.key === val.type) {
                rv.value ++;
            }
        });
    });

    return result;
}

export function getPaymentTypeStatisticData(trades) {
    let result = [{
        title: '微信支付',
        key: 'WEIXIN',
        value: 0
    }, {
        title: '微信代销',
        key: 'WEIXIN_DAIXIAO',
        value: 0
    }, {
        title: '支付宝',
        key: 'ALIPAY',
        value: 0
    }, {
        title: '银行卡',
        key: 'BANKCARDPAY',
        value: 0
    }, {
        title: '代付',
        key: 'PEERPAY',
        value: 0
    }, {
        title: '货到付款',
        key: 'CODPAY',
        value: 0
    }, {
        title: '百度钱包',
        key: 'BAIDUPAY',
        value: 0
    }, {
        title: '直接领取赠品',
        key: 'PRESENTTAKE',
        value: 0
    }, {
        title: '优惠券/码全额抵扣',
        key: 'COUPONPAY',
        value: 0
    }, {
        title: '分销商采购',
        key: 'BULKPURCHASE',
        value: 0
    }, {
        title: '有赞E卡通',
        key: 'ECARD',
        value: 0
    }, {
        title: '其他渠道',
        key: 'OTHERS',
        value: 0
    }];

    trades.forEach((val, index) => {
        result.forEach((rv, ri) => {
            if (val.pay_type && rv.key === val.pay_type) {
                rv.value ++;
            }
        });
    });

    return result;
}

export function getStatusStatisticData(trades) {
    let result = [{
        title: '没有创建支付交易',
        key: 'TRADE_NO_CREATE_PAY',
        value: 0
    }, {
        title: '等待买家付款',
        key: 'WAIT_BUYER_PAY',
        value: 0
    }, {
        title: '等待支付确认',
        key: 'WAIT_PAY_RETURN',
        value: 0
    }, {
        title: '付款以前，卖家或买家主动关闭交易',
        key: 'TRADE_CLOSED_BY_USER',
        value: 0
    }, {
        title: '等待成团',
        key: 'WAIT_GROUP',
        value: 0
    }, {
        title: '等待卖家发货',
        key: 'WAIT_SELLER_SEND_GOODS',
        value: 0
    }, {
        title: '等待买家确认收货',
        key: 'WAIT_BUYER_CONFIRM_GOODS',
        value: 0
    }, {
        title: '买家已签收',
        key: 'TRADE_BUYER_SIGNED',
        value: 0
    }, {
        title: '付款以后用户退款成功，交易自动关闭',
        key: 'TRADE_CLOSED',
        value: 0
    }, {
        title: '已退款',
        key: 'REFUND',
        value: 0
    }];

    trades.forEach((val, index) => {
        result.forEach((rv, ri) => {
            if (val.status && rv.key === val.status) {
                rv.value ++;
            }
        });
    });

    return result;
}

export function getGoodsSoldStatisticData(trades) {
    let goodsMap = new Map();
    trades.forEach((v, i) => {
        if (v['orders']) {
            v['orders'].forEach((ov, oi) => {
                if (goodsMap.has(ov.num_iid)) {
                    goodsMap.set(ov.num_iid, {
                        title: ov.title,
                        id: ov.num_iid,
                        count: goodsMap.get(ov.num_iid)['count'] + parseInt(ov.num, 10)
                    });
                } else {
                    goodsMap.set(ov.num_iid, {
                        title: ov.title,
                        id: ov.num_iid,
                        count: parseInt(ov.num, 10)
                    });
                }
            });
        }
    });

    let result = [];
    for (let val of goodsMap.values()) {
        result.push(val);
    }

    return result;
}

export function getAreaStatisticData(trades) {
    let cityMap = new Map();
    trades.forEach((v, i) => {
        console.log(v.receiver.city, CityLatLng[v.receiver.city]);
        if (v.receiver && v.receiver.city && CityLatLng[v.receiver.city]) {
            if (cityMap.get(v.receiver.city)) {
                cityMap.set(v.receiver.city, {
                    name: v.receiver.city,
                    latlng: CityLatLng[v.receiver.city],
                    count: cityMap.get(v.receiver.city)['count'] + 1
                });
            } else {
                cityMap.set(v.receiver.city, {
                    name: v.receiver.city,
                    latlng: CityLatLng[v.receiver.city],
                    count: 1
                });
            }
        }
    });

    let result = [];
    for (let v of cityMap.values()) {
        result.push({
            name: v.name,
            value: [v.latlng[0], v.latlng[1], v.count]
        });
    }

    return result;
}

export function getTradeAmount(trades) {
    let amount = 0;
    trades.forEach((v, i) => {
        amount += parseFloat(v.total_fee, 0.0);
    });
    return amount.toFixed(2);
}

export function getPaymentAmount(trades) {
    let amount = 0;
    trades.forEach((v, i) => {
        if (v.payment && v.pay_time) {
            amount += parseFloat(v.payment, 0.0);
        }
    });
    return amount.toFixed(2);
}

export function removeBlankValue(data) {
    let result = [];
    data.forEach((v, i) => {
        if (v.value > 0 || v.count > 0) {
            result.push(v);
        }
    });

    return result;
}